package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
import com.example.demo.WuYouPushOrderInfo;
import com.example.domain.YXOrderCancelResult;
import com.example.ratelimit.BaseResponse;
import com.example.ratelimit.MallResponse;
import com.example.ratelimit.RateLimitTestService;
import com.example.util.IpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @program: demo
 * @description: 日志整合
 * @author: Mr.Wang
 * @create: 2019-05-22 14:01
 **/
@RestController
@RequestMapping("/demo/log")
public class LogBackController {
    private Logger logger = LoggerFactory.getLogger(LogBackController.class);

    @Autowired
    RateLimitTestService rateLimitTestService;

    @GetMapping(value = "/rate/limit")
    public MallResponse rateLimit() throws InterruptedException {
        String s = rateLimitTestService.rateLimit("1111");
        MallResponse mallResult = new MallResponse();
        mallResult.setCurrentSystemTimeStamp(System.currentTimeMillis() + "");
        mallResult.setData(s);
        BaseResponse baseResponse = new BaseResponse();
        if(StringUtils.isEmpty(s)){
            baseResponse.setCode("10086");
            baseResponse.setMsg("当前查看协议人数过多，请稍后重试哟");
        }else{
            baseResponse.setCode("200");
            baseResponse.setMsg("成功");
        }
        mallResult.setBaseResponse(baseResponse);
        return mallResult;
    }

    @PostMapping(value = "/testLog")
    public JSONObject testLog(@RequestBody String json) {
//        logger.info("测试log back日志======json:{}", json);
        JSONObject jsonObject = JSONObject.parseObject(json);
        jsonObject.put("no", "aaa");
        System.out.println(jsonObject.toString());
        List<String> list = (List<String>) jsonObject.get("list");
        list.forEach(li -> {
            System.out.println("============" + li);
        });
        return jsonObject;
    }

    @PostMapping(value = "/aaa")
    public WuYouPushOrderInfo aaa(@RequestBody WuYouPushOrderInfo info) {
        System.out.println(info.toString());
        return info;
    }

    @GetMapping(value ="/get/ip")
    public String getIp() {
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attr.getRequest();
        IpUtil.getRealIp(request);
        return "";
    }

    @PostMapping(value = "/cancel/order")
    private String cancelOrderCallback(@RequestParam Map<String, String> map) {

        if (null == map) {
            return "null";
        }
        String orderCancelResult = map.get("orderCancelResult");
        JSONObject jsonObject = JSONObject.parseObject(orderCancelResult);
        String rejectReason = jsonObject.getString("rejectReason");
        String orderId = jsonObject.getString("orderId");
        Integer cancelStatus = jsonObject.getInteger("cancelStatus");
        System.out.println(rejectReason + "==" + orderId + "==" + cancelStatus);
        System.out.println(orderCancelResult);

        return orderCancelResult;
    }

}
